package gz.gae.blog.action;

import gz.gae.blog.BlogManager;
import gz.gae.blog.entity.Blog;
import gz.gae.blog.ioc.WebBlogUtil;

/**
 * User: Kane Gong
 * Date: Nov 27, 2010
 * Time: 1:59:12 PM
 */
public class BlogAction
        extends AuthedAction {
    long bId;
    String content;
    String subject;
    String permission;

    public long getBId() {
        return bId;
    }

    public void setBId(long bId) {
        this.bId = bId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public String add() throws Exception {
        BlogManager bm = WebBlogUtil.getBlogManager();
        Blog blog = new Blog();
        blog.setOwner(getUser());
        blog.setSubject(subject);
        blog.setContent(content);
        blog.setPermission(Blog.Permission.valueOf(permission));
        bm.add(blog);
        bm.close();
        return SUCCESS;
    }

    public String update() throws Exception {
        BlogManager bm = WebBlogUtil.getBlogManager();
        Blog blog = bm.get(bId);
        blog.setOwner(getUser());
        blog.setSubject(subject);
        blog.setContent(content);
        blog.setPermission(Blog.Permission.valueOf(permission));
        bm.update(blog);
        bm.close();
        return SUCCESS;
    }

    public String delete() throws Exception {
        BlogManager bm = WebBlogUtil.getBlogManager();
        bm.deleteByOwner(bId, getUser());
        bm.close();
        return SUCCESS;
    }
}
